package com.ydw;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Yangdw
 * @date 2025-09-01
 * @description:
 */
public class Producer {

    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("test-filter-group",true);

        producer.setNamesrvAddr("127.0.0.1:9876");

        producer.start();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<Message>messages = new ArrayList<>();
        List<Order> orderList1 = buildList("1", "Buy", "Pay", "Finish");

        List<Order> orderList2 = buildList("2", "Buy", "Pay");
        List<Order> orderList3 = buildList("3",  "Pay", "Finish");


        List<Order> allList = new ArrayList<>(9);
        allList.addAll(orderList1);
        allList.addAll(orderList2);
        allList.addAll(orderList3);
        for (int i = 0; i < allList.size(); i++) {
            Order order = allList.get(i);
            Message message = new Message(
                    "custom-filter-topic",
                    order.getDesc(),
                    JSONObject.toJSONString(order).getBytes(StandardCharsets.UTF_8)
            );

            //sql92 可以根据这个属性来过滤
            message.putUserProperty("orderId",order.orderId);
            messages.add(message);

        }
        producer.send(messages);
    }


    public static List<Order> buildList(String orderId,String ...descList){
        List<Order> orderList = new ArrayList<>(3);
        for (String s : descList) {
            Order order = new Order(orderId,s);
            orderList.add(order);
        }
        return orderList;
    }

    static class Order{
        private String orderId;
        private String desc;

        public Order(String orderId, String desc) {
            this.orderId = orderId;
            this.desc = desc;
        }

        public String getOrderId() {
            return orderId;
        }

        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
